import { WebManageConst } from "src/consts/web_manage_const";
import { WebVisitConst } from "src/consts/web_visit_const";
import { PopupName } from "src/consts/popup_const";
import * as Api from 'src/api/net';

const state: any = {} as any;
// StateIniter.init(state, 'demo');

state.model = []
state.selection = []
state.website_id = ""
// vuex

/** 更新数据的 */
const mutations = {
    [WebManageConst.CommitWebMSetList](state: any, data: any): void {
        console.log("webM_Set_Panel data update", data);
        state.model = data.formData
        state.website_id = data.website_id

        var selectArr = []
        for (let i = 0; i < data.selection.length; i++) {
            selectArr.push(data.selection[i].owner_id)
        }
        state.selection = selectArr

        WLLib.panel(PopupName.WebM_Set_Panel);
    },
}

//异步处理
const actions = {
    [WebManageConst.DispatchWebMSet](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/webapply/show/website.do", data, (res: any) => {
            let senData: any = {}
            senData["curr_page"] = 1
            senData["page_size"] = 99999
            Api.AjaxPost(Config.ajaxUrl + "/account/normal/list.do", senData, (accRes: any) => {
                if (isNoEmptyObject(accRes.data)) {
                    var allData: any = {}
                    allData.formData = accRes.data.data_list
                    allData.selection = res.data
                    allData.website_id = data.id
                    WLCommit(WebManageConst.CommitWebMSetList, allData);
                }
            })
        })
    },
    [WebManageConst.DispatchWebMSetEdit](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/webapply/save.do", data, (res: any) => {
            WLMessage("授权成功", "success")
            WLLib.panel(PopupName.WebM_Set_Panel).close();

            // let sendData: any = {}
            // sendData["curr_page"] = 1
            // sendData["page_size"] = 15
            // sendData.id = getUserInfo().userId
            // WLDispatch(WebVisitConst.DispatchWebVList, sendData);
        })
    }
}

export default {
    state,
    mutations,
    actions
}